//=============================================================================
/**
 *
 *	@file		encount_effect_rocket.c
 *	@brief		エンカウントエフェクト(ロケット団)
 *	@author		toru nagihasi >> hosaka genya
 *	@data		2009.02.25	オンメモリが足りないので encount_effect_sub から切り出し
 *
 */
//=============================================================================
#include "common.h"
#include "system/lib_pack.h"
#include "system/brightness.h"
#include "fieldsys.h"
#include "fieldmap_work.h"
#include "field_event.h"
#include "fld_motion_bl.h"
#include "encount_effect_def.h"
#include "system/wipe.h"
#include "system/laster.h"
#include "system/fontproc.h"
#include "field_encounteffect.naix"
#include "savedata/mystatus.h"
#include "include/battle/trtype_def.h"
#include "include/battle/trno_def.h"
#include "include/msgdata/msg.naix"
#include "include/msgdata/msg_encount_effect.h"

#include "field/fieldmap.h"

#include "encount_effect_rocket.h"

#define FLUSH_SET( sync, to, from )	{	\
	ChangeBrightnessRequest( sync, to, from, \
	( GX_BLEND_PLANEMASK_OBJ | GX_BLEND_PLANEMASK_BG1 | GX_BLEND_PLANEMASK_BG2 | GX_BLEND_PLANEMASK_BG3 ), \
	MASK_MAIN_DISPLAY ); \
} \

//-----------------------------------------------------------------------------
/**
 *					コーディング規約
 *		●関数名
 *				１文字目は大文字それ以降は小文字にする
 *		●変数名
 *				・変数共通
 *						constには c_ を付ける
 *						staticには s_ を付ける
 *						ポインタには p_ を付ける
 *						全て合わさると csp_ となる
 *				・グローバル変数
 *						１文字目は大文字
 *				・関数内変数
 *						小文字と”＿”と数字を使用する 関数の引数もこれと同じ
*/
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/**
 *
 *		ロケット団	エンカウント [ ROCKET ]
 *
 */
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/**
 *					定数宣言
*/
//-----------------------------------------------------------------------------
//-------------------------------------
//	ロケット団通常
//=====================================
// シーケンス
enum{
	ENCOUNT_ROCKET_NORMAL_START,
	ENCOUNT_ROCKET_NORMAL_FLASH_INI,
	ENCOUNT_ROCKET_NORMAL_FLASH_WAIT,
	ENCOUNT_ROCKET_NORMAL_OAM,
	ENCOUNT_ROCKET_NORMAL_OAM_WAIT,
	ENCOUNT_ROCKET_NORMAL_FLASHOUT,
	ENCOUNT_ROCKET_NORMAL_FLASHOUT_WAIT,
	ENCOUNT_ROCKET_NORMAL_END,
};
// OAMデータ
// アクターデータ数
#define ENCOUNT_ROCKET_NORMAL_CELL_NUM		( 6 )	// ワーク数
#define ENCOUNT_ROCKET_NORMAL_CELL_RES_NUM	( 1 )	// リソース数
#define ENCOUNT_ROCKET_CELL_CONT_ID	( 600000 )

// 動さパラメータ	（触ってよい部分）
#define ENCOUNT_ROCKET_NORMAL_OAM_MOVE_SYNC	( 8 )	// OAM1つ動さシンク
#define ENCOUNT_ROCKET_NORMAL_FLASHOUT_SYNC	( 12 )	// フラッシュアウトシンク

#define ENCOUNT_ROCKET_NORMAL_OAM_S_SCA	( FX32_CONST(2.0f) )	//	開始スケール
#define ENCOUNT_ROCKET_NORMAL_OAM_E_SCA	( FX32_CONST(0.01f) )	//	終了スケール
#define ENCOUNT_ROCKET_NORMAL_OAM_SS_SCA	( -FX32_CONST(0.40f) )	//	初速度スケール

enum{
	ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SX,
	ENCOUNT_ROCKET_NORMAL_OAM_PARAM_EX,
	ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SSX,
	ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SY,
	ENCOUNT_ROCKET_NORMAL_OAM_PARAM_EY,
	ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SSY,
	ENCOUNT_ROCKET_NORMAL_OAM_PARAM_WAIT,
	ENCOUNT_ROCKET_NORMAL_OAM_PARAM_ROTA,
	ENCOUNT_ROCKET_NORMAL_OAM_PARAM_NUM,
};

// ボール動さパラメータ
// ENCOUNT_ROCKET_NORMAL_CELL_NUM　と下のテーブル数が一緒なら
// 何個にでも出来ます
static const s32 ENCOUNT_ROCKET_NORMAL_OAM_Param[ ENCOUNT_ROCKET_NORMAL_CELL_NUM ][ ENCOUNT_ROCKET_NORMAL_OAM_PARAM_NUM ] = {
	{	// 0
		FX32_CONST( 260 ),		// 開始X座標
        FX32_CONST( 128 ),		// 終了X座標
        -FX32_CONST( 30 ),		// 初速度X
		FX32_CONST( 0 ),		// 開始Y座標
		FX32_CONST( 100 ),		// 終了Y座標
		FX32_CONST( 20 ),		// 初速度Y
		4,						// 開始ウエイト
		0xffff * 2				// 回転量
	},
	{// 1
		FX32_CONST( -16 ),		// 開始X座標
		FX32_CONST( 128 ),      // 終了X座標
		FX32_CONST( 30 ),       // 初速度X
		FX32_CONST(160),        // 開始Y座標
		FX32_CONST(100),        // 終了Y座標
		-FX32_CONST(20),		// 初速度Y
		3,						// 開始ウエイト
		0xffff * 1				// 回転量
	},
	{// 2
		FX32_CONST(0),			// 開始X座標
		FX32_CONST(128),         // 終了X座標
		FX32_CONST(30),         // 初速度X
		FX32_CONST(-16),        // 開始Y座標
		FX32_CONST(100),         // 終了Y座標
		FX32_CONST(20),         // 初速度Y
		4,                       // 開始ウエイト
		-0xffff * 3				// 回転量
	},
	{// 3
		FX32_CONST(140),			// 開始X座標
		FX32_CONST(128),         // 終了X座標
		-FX32_CONST(10),         // 初速度X
		FX32_CONST(160),        // 開始Y座標
		FX32_CONST(100),         // 終了Y座標
		-FX32_CONST(20),         // 初速度Y
		2,                       // 開始ウエイト
		-0xffff * 2				// 回転量
	},
	{// 4
		FX32_CONST(260),			// 開始X座標
		FX32_CONST(128),         // 終了X座標
		-FX32_CONST(30),         // 初速度X
		FX32_CONST(80),        // 開始Y座標
		FX32_CONST(100),         // 終了Y座標
		FX32_CONST(1),         // 初速度Y
		3,                       // 開始ウエイト
		-0xffff * 3				// 回転量
	},
	{// 5
		FX32_CONST(0),			// 開始X座標
		FX32_CONST(128),         // 終了X座標
		FX32_CONST(30),         // 初速度X
		FX32_CONST(160),        // 開始Y座標
		FX32_CONST(100),         // 終了Y座標
		-FX32_CONST(20),         // 初速度Y
		3,                       // 開始ウエイト
		0xffff * 1				// 回転量
	},
};

//-------------------------------------
//	ロケット団　幹部クラス
//=====================================
// シーケンス
enum{
	ENCOUNT_ROCKET_SPECIAL_START,
	ENCOUNT_ROCKET_SPECIAL_SCROLL_INI,
	ENCOUNT_ROCKET_SPECIAL_SCROLL,
	ENCOUNT_ROCKET_SPECIAL_MASK_INI,
	ENCOUNT_ROCKET_SPECIAL_MASK,
	ENCOUNT_ROCKET_SPECIAL_MASKWAIT,
	ENCOUNT_ROCKET_SPECIAL_ALPHA_INI,
	ENCOUNT_ROCKET_SPECIAL_CUTIN,
	ENCOUNT_ROCKET_SPECIAL_CUTFLASH,
	ENCOUNT_ROCKET_SPECIAL_CUTWAIT,
	ENCOUNT_ROCKET_SPECIAL_CUTOUT,
	ENCOUNT_ROCKET_SPECIAL_WHITEOUT,
	ENCOUNT_ROCKET_SPECIAL_END,
};
// OAMデータ
// アクターデータ数
#define ENCOUNT_ROCKET_SPECIAL_CELL_NUM		( 1 )	// ワーク数
#define ENCOUNT_ROCKET_SPECIAL_CELL_RES_NUM	( 1 )	// リソース数
#define ENCOUNT_ROCKET_SPECIAL_CELL_CONT_ID	( 600000 )

// ビットマップ関係
#define ENCOUNT_ROCKET_SPECIAL_BMP_COL	( 15 )

//-----------------------------------------------------------------------
// 動作パラメータ
//-----------------------------------------------------------------------
// BGスクロールエフェクト関係
#define ENCOUNT_ROCKET_SPECIAL_SCROLL_SPEED	( 24 )
#define ENCOUNT_ROCKET_SPECIAL_SCROLL_SYNC	( 12 )

// マスク
#define ENCOUNT_ROCKET_SPECIAL_MASK_SYNC	( 4 )
#define ENCOUNT_ROCKET_SPECIAL_MASKWAIT_SYNC ( 13 )

#define ENCOUNT_ROCKET_SPECIAL_ALPHA_PLANE_1 ( GX_BLEND_PLANEMASK_BG1 )
#define ENCOUNT_ROCKET_SPECIAL_ALPHA_PLANE_2 ( GX_BLEND_PLANEMASK_BG0 | GX_BLEND_PLANEMASK_BG2 | GX_BLEND_PLANEMASK_BG3 | GX_BLEND_PLANEMASK_OBJ | GX_BLEND_PLANEMASK_BD )
#define ENCOUNT_ROCKET_SPECIAL_ALPHA_SPEED ( 4 )

#define ENCOUNT_ROCKET_SPECIAL_CUTFLUSH_SYNC ( 8 )
#define ENCOUNT_ROCKET_SPECIAL_CUTWAIT_SYNC ( 16 )	///< カットインウェイト
#define ENCOUNT_ROCKET_SPECIAL_CUTOUT_SPEED ( 8 )	///< カットアウト

// OBJ関連
#define ENCOUNT_ROCKET_SPECIAL_MAT_X		( (64+256)*FX32_ONE )	// 開始座標
#define ENCOUNT_ROCKET_SPECIAL_MAT_Y		( 16*8*FX32_ONE )		// 開始座標

// トレーナー名
#define ENCOUNT_SPTR_GYM_TRNAME_X		( 0 )	// トレーナー名Xキャラ座標
#define ENCOUNT_SPTR_GYM_TRNAME_Y		( 20 )	// トレーナー名Yキャラ座標
#define ENCOUNT_SPTR_GYM_TRNAME_SIZX	( 16 )	// トレーナー名BMPXキャラサイズ
#define ENCOUNT_SPTR_GYM_TRNAME_SIZY	( 2 )	// トレーナー名BMPYキャラサイズ
#define ENCOUNT_SPTR_GYM_TRNAME_CGX		( 1 )
#define ENCOUNT_SPTR_GYM_TRNAME_PAL		( 2 )
#define ENCOUNT_SPTR_GYM_TRNAME_COL		( GF_PRINTCOLOR_MAKE( 1, 2, 0 ) )

#define ENCOUNT_SPTR_GYM_TRNAME_OFS_X	( 8 )				// OAMの位置から見てTRNAMEをどの位置に表示するか

//-----------------------------------------------------------------------------
/**
 *					構造体宣言
*/
//-----------------------------------------------------------------------------
//-------------------------------------
//	ロケット団通常
//=====================================
typedef struct {
	ENC_CLACT_SYS	clact_sys;
	ENC_CLACT_RES_WORK clact_res;
	CLACT_WORK_PTR clact[ ENCOUNT_ROCKET_NORMAL_CELL_NUM ];
	ENC_ADDMOVE_WORK_FX oam_scale[ ENCOUNT_ROCKET_NORMAL_CELL_NUM ];
	ENC_ADDMOVE_WORK_FX oam_move_x[ ENCOUNT_ROCKET_NORMAL_CELL_NUM ];
	ENC_ADDMOVE_WORK_FX oam_move_y[ ENCOUNT_ROCKET_NORMAL_CELL_NUM ];
	ENC_MOVE_WORK		oam_rota[ ENCOUNT_ROCKET_NORMAL_CELL_NUM ];
	BOOL oam_move_flg[ ENCOUNT_ROCKET_NORMAL_CELL_NUM ];

	s32 count;
	s32 wait;
} ENCOUNT_ROCKET_NORMAL;

//-------------------------------------
//	ロケット団　幹部クラス
//=====================================
typedef struct {
	CLACT_WORK_PTR clact;
	ENC_CLACT_SYS	clact_sys;
	ENC_CLACT_RES_WORK clact_res;
	ENC_ADDMOVE_WORK_FX addmove_fx;
	ENCOUNT_LASTER_SCROLL laster;

	GF_BGL_BMPWIN trnamewin;	///< トレーナー名表示用

	VecFx32 act_vec;
	s32 wait;
	BOOL laster_flg;
} ENCOUNT_ROCKET_SPECIAL;

//--------------------------------------------------------------
///	それぞれで変わるところ
//==============================================================
typedef struct {
	u8 nclr;
	u8 ncgr;
	u8 ncer;
	u8 nanr;
	int trno;	///< トレーナータイプ
} ENCOUNT_RSP_PARAM;

static const ENCOUNT_RSP_PARAM ENCOUNT_RSP_Def[ 5 ] = {
	// ramuda
	{
		NARC_field_encounteffect_r_kanbu_obj_NCLR,
		NARC_field_encounteffect_r_kanbu_obj_NCGR,
		NARC_field_encounteffect_r_kanbu_obj_NCER,
		NARC_field_encounteffect_r_kanbu_obj_NANR,
		RLEADER04_01,
	},
	// lance
	{
		NARC_field_encounteffect_r_kanbu2_obj_NCLR,
		NARC_field_encounteffect_r_kanbu2_obj_NCGR,
		NARC_field_encounteffect_r_kanbu2_obj_NCER,
		NARC_field_encounteffect_r_kanbu2_obj_NANR,
		RLEADER03_01,
	},
	// aporo
	{
		NARC_field_encounteffect_r_kanbu3_obj_NCLR,
		NARC_field_encounteffect_r_kanbu3_obj_NCGR,
		NARC_field_encounteffect_r_kanbu3_obj_NCER,
		NARC_field_encounteffect_r_kanbu3_obj_NANR,
		RLEADER01_01,
	},
	// atena
	{
		NARC_field_encounteffect_r_kanbu4_obj_NCLR,
		NARC_field_encounteffect_r_kanbu4_obj_NCGR,
		NARC_field_encounteffect_r_kanbu4_obj_NCER,
		NARC_field_encounteffect_r_kanbu4_obj_NANR,
		RLEADER02_01,
	},
	// sakaki
	{
		NARC_field_encounteffect_r_kanbu5_obj_NCLR,
		NARC_field_encounteffect_r_kanbu5_obj_NCGR,
		NARC_field_encounteffect_r_kanbu5_obj_NCER,
		NARC_field_encounteffect_r_kanbu5_obj_NANR,
		RBOSS_01,
	},
};

//----------------------------------------------------------------------------
/**
 *	@brief	ロケット団　通常
 */
//-----------------------------------------------------------------------------
void EncountEffect_Rocket_Normal(TCB_PTR tcb, void* work)
{
	ENCOUNT_EFFECT_WORK *eew = work;
	ENCOUNT_ROCKET_NORMAL* task_w = eew->work;
	BOOL result;
	int i;
	VecFx32 matrix;
	VecFx32 scale;

	switch( eew->seq ){
	case ENCOUNT_ROCKET_NORMAL_START:
		eew->work = sys_AllocMemory( HEAPID_FIELD, sizeof(ENCOUNT_ROCKET_NORMAL) );
		memset( eew->work, 0, sizeof(ENCOUNT_ROCKET_NORMAL) );
		task_w = eew->work;

		// セルアクターセット作成
		ENC_CLACT_Init( &task_w->clact_sys, ENCOUNT_ROCKET_NORMAL_CELL_NUM, ENCOUNT_ROCKET_NORMAL_CELL_RES_NUM );

		// グラフィック設定
		ENC_CLACT_ResLoadEasy(
				eew->p_handle,
				&task_w->clact_sys,
				&task_w->clact_res,
				NARC_field_encounteffect_rocket_NCLR, 1,
				NARC_field_encounteffect_rocket_small_NCGR,
				NARC_field_encounteffect_rocket_small_NCER,
				NARC_field_encounteffect_rocket_small_NANR,
				ENCOUNT_ROCKET_CELL_CONT_ID
				);

		// アクター登録
		for( i=0; i<ENCOUNT_ROCKET_NORMAL_CELL_NUM; i++ ){
			task_w->clact[i] = ENC_CLACT_Add(
					&task_w->clact_sys, &task_w->clact_res,
					0,
					0,0, 0);
			CLACT_SetDrawFlag( task_w->clact[i], FALSE );
		}
		GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );

		eew->seq ++;
		break;

	case ENCOUNT_ROCKET_NORMAL_FLASH_INI:

		EncountFlashTask(MASK_MAIN_DISPLAY, -16, -16,  &eew->wait, 2);
		eew->seq++;
		break;

	case ENCOUNT_ROCKET_NORMAL_FLASH_WAIT:
		if( eew->wait ){
			eew->seq++;
			task_w->count = 0;
			task_w->wait = ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_WAIT];
		}
		break;

	case ENCOUNT_ROCKET_NORMAL_OAM:
		task_w->wait --;
		if( task_w->wait < 0 ){
			ENC_AddMoveReqFx( &task_w->oam_move_x[task_w->count],
					ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SX],
					ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_EX],
					ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SSX],
					ENCOUNT_ROCKET_NORMAL_OAM_MOVE_SYNC );

			ENC_AddMoveReqFx( &task_w->oam_move_y[task_w->count],
					ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SY],
					ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_EY],
					ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SSY],
					ENCOUNT_ROCKET_NORMAL_OAM_MOVE_SYNC );

			ENC_AddMoveReqFx( &task_w->oam_scale[task_w->count],
					ENCOUNT_ROCKET_NORMAL_OAM_S_SCA,
					ENCOUNT_ROCKET_NORMAL_OAM_E_SCA,
					ENCOUNT_ROCKET_NORMAL_OAM_SS_SCA,
					ENCOUNT_ROCKET_NORMAL_OAM_MOVE_SYNC );

			ENC_MoveReq( &task_w->oam_rota[task_w->count],
					0,
					ENCOUNT_ROCKET_NORMAL_OAM_Param[ task_w->count ][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_ROTA],
					ENCOUNT_ROCKET_NORMAL_OAM_MOVE_SYNC );

			CLACT_SetDrawFlag( task_w->clact[task_w->count], TRUE );
			matrix = ENC_MakeVec(
					ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SX],
					ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_SY], 0 );
			CLACT_SetMatrix( task_w->clact[task_w->count], &matrix );
			scale = ENC_MakeVec(
					ENCOUNT_ROCKET_NORMAL_OAM_S_SCA,
					ENCOUNT_ROCKET_NORMAL_OAM_S_SCA, 0 );
			CLACT_SetScaleAffine( task_w->clact[task_w->count], &scale, CLACT_AFFINE_DOUBLE );

			task_w->oam_move_flg[task_w->count] = TRUE;

			task_w->count++;

			if( task_w->count >= ENCOUNT_ROCKET_NORMAL_CELL_NUM ){
				eew->seq++;
			}else{
				task_w->wait = ENCOUNT_ROCKET_NORMAL_OAM_Param[task_w->count][ENCOUNT_ROCKET_NORMAL_OAM_PARAM_WAIT];
			}
		}
		break;

	case ENCOUNT_ROCKET_NORMAL_OAM_WAIT:
		// 全動さ完了チェック
		if( task_w->oam_move_flg[ ENCOUNT_ROCKET_NORMAL_CELL_NUM - 1 ] == FALSE ){
			eew->seq ++;
		}
		break;

	case ENCOUNT_ROCKET_NORMAL_FLASHOUT:
		// フィールドHブランクOFF
		FLDHBLANK_SYS_Stop( eew->fsw->fldmap->hblanksys );

		WIPE_SYS_Start( WIPE_PATTERN_M, WIPE_TYPE_FLASHOUT_FOREWAY, WIPE_TYPE_FADEOUT, WIPE_FADE_BLACK, ENCOUNT_ROCKET_NORMAL_FLASHOUT_SYNC, 1, HEAPID_FIELD );
		eew->seq++;
		break;

	case ENCOUNT_ROCKET_NORMAL_FLASHOUT_WAIT:
		if( WIPE_SYS_EndCheck() ){
			eew->seq++;
		}
		break;

	case ENCOUNT_ROCKET_NORMAL_END:
		WIPE_SetBrightness( WIPE_DISP_SUB, WIPE_FADE_BLACK );

		// フィールドHブランクON
		FLDHBLANK_SYS_Start( eew->fsw->fldmap->hblanksys );

		if(eew->end != NULL){
			*(eew->end) = TRUE;		// タスク終了を報告
		}
		// OAM破棄
		for( i=0; i<ENCOUNT_ROCKET_NORMAL_CELL_NUM; i++ ){
			CLACT_Delete( task_w->clact[i] );
		}
		ENC_CLACT_ResDeleteEasy( &task_w->clact_sys, &task_w->clact_res );
		ENC_CLACT_Delete( &task_w->clact_sys );

		ENC_End( eew, tcb );
		break;
	}


	// オブジェクト動さ
	for( i=0; i<ENCOUNT_ROCKET_NORMAL_CELL_NUM; i++ ){

		if( task_w->oam_move_flg[i] == TRUE ){

			result = ENC_AddMoveMainFx( &task_w->oam_move_x[i] );
			ENC_AddMoveMainFx( &task_w->oam_move_y[i] );
			ENC_AddMoveMainFx( &task_w->oam_scale[i] );
			ENC_MoveMain( &task_w->oam_rota[i] );
			if( result ){
				task_w->oam_move_flg[i] = FALSE;
				CLACT_SetDrawFlag( task_w->clact[i], FALSE );
			}
			matrix = ENC_MakeVec(
					task_w->oam_move_x[i].x,
					task_w->oam_move_y[i].x, 0 );
			CLACT_SetMatrix( task_w->clact[i], &matrix );
			scale = ENC_MakeVec(
					task_w->oam_scale[i].x,
					task_w->oam_scale[i].x, 0 );
			CLACT_SetScale( task_w->clact[i], &scale );
			CLACT_SetRotation( task_w->clact[i], task_w->oam_rota[i].x );
		}
	}

	if( eew->seq != ENCOUNT_ROCKET_NORMAL_END ){
		CLACT_Draw( task_w->clact_sys.cas );
	}
}

//----------------------------------------------------------------------------
/**
 *	@brief	ロケット団　幹部クラス
 */
//-----------------------------------------------------------------------------
static void EncountEffect_RSp_Main( TCB_PTR tcb, void* work, const ENCOUNT_RSP_PARAM* cp_def );

//-----------------------------------------------------------------------------
/**
 *	@brief	スクリーン上書き
 *
 *	@param	ENCOUNT_EFFECT_WORK* eew
 *	@param	bg_frm
 *	@param	scr_idx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
static void Scrn_OverWrite( ENCOUNT_EFFECT_WORK* eew, u8 bg_frm, u32 scr_idx )
{
	void* buff;
	NNSG2dScreenData* p_scrn;

	// スクリーンデータ読み込み
	buff = ArcUtil_HDL_ScrnDataGet( eew->p_handle, scr_idx, FALSE, &p_scrn, HEAPID_FIELD );
	// スクリーンデータ書き込み
	GF_BGL_ScrWrite( eew->fsw->bgl, bg_frm,
			p_scrn->rawData, 0, 0, p_scrn->screenWidth / 8, p_scrn->screenHeight / 8 );

	sys_FreeMemoryEz( buff );

	// スクリーンデータ転送
	GF_BGL_LoadScreenReq( eew->fsw->bgl, bg_frm );
}

//----------------------------------------------------------------------------
/**
 *	@brief	トレーナータイプから名前を取得する
 *
 *	@param	type		トレーナータイプ
 *	@param	heapID		ヒープID
 *
 *	@return	名前	破棄が必要　STRBUF_Delete()
 */
//-----------------------------------------------------------------------------
static STRBUF* EncountEffect_Rocket_TrTypeNameGet( u32 type, HEAPID heapID )
{
	WORDSET* p_wordset;
	MSGDATA_MANAGER* p_man;
	STRBUF* p_str;
	STRBUF* p_tmp;

	p_man = MSGMAN_Create(MSGMAN_TYPE_DIRECT, ARC_MSG, NARC_msg_encount_effect_dat, heapID);
	p_wordset = WORDSET_Create( heapID );
	p_str = STRBUF_Create( 128, heapID );
	p_tmp = STRBUF_Create( 128, heapID );
	MSGMAN_GetString( p_man, msg_00, p_tmp );
	WORDSET_RegisterTrainerName( p_wordset, 0, type );
	WORDSET_ExpandStr( p_wordset, p_str, p_tmp );

	MSGMAN_Delete( p_man );
	WORDSET_Delete( p_wordset );
	STRBUF_Delete( p_tmp );
	return p_str;
}

// 公開関数
void EncountEffect_Rocket_Ramuda(TCB_PTR tcb, void* work)
{
	EncountEffect_RSp_Main( tcb, work, &ENCOUNT_RSP_Def[0] );
}

void EncountEffect_Rocket_Lance(TCB_PTR tcb, void* work)
{
	EncountEffect_RSp_Main( tcb, work, &ENCOUNT_RSP_Def[1] );
}

void EncountEffect_Rocket_Aporo(TCB_PTR tcb, void* work)
{
	EncountEffect_RSp_Main( tcb, work, &ENCOUNT_RSP_Def[2] );
}

void EncountEffect_Rocket_Atena(TCB_PTR tcb, void* work)
{
	EncountEffect_RSp_Main( tcb, work, &ENCOUNT_RSP_Def[3] );
}

void EncountEffect_Rocket_Sakaki(TCB_PTR tcb, void* work)
{
	EncountEffect_RSp_Main( tcb, work, &ENCOUNT_RSP_Def[4] );
}

static void EncountEffect_RSp_Main( TCB_PTR tcb, void* work, const ENCOUNT_RSP_PARAM* cp_def )
{
	HEAPID heapID = HEAPID_FIELD;
	ENCOUNT_EFFECT_WORK *eew = work;
	ENCOUNT_ROCKET_SPECIAL* task_w = eew->work;

	switch( eew->seq ){
	case ENCOUNT_ROCKET_SPECIAL_START:
		eew->work = sys_AllocMemory( HEAPID_FIELD, sizeof(ENCOUNT_ROCKET_SPECIAL) );
		memset( eew->work, 0, sizeof(ENCOUNT_ROCKET_SPECIAL) );
		task_w = eew->work;

		// セルアクターセット作成
		ENC_CLACT_Init( &task_w->clact_sys, ENCOUNT_ROCKET_SPECIAL_CELL_NUM, ENCOUNT_ROCKET_SPECIAL_CELL_RES_NUM );

		// グラフィック設定
		ENC_CLACT_ResLoadEasy(
				eew->p_handle,
				&task_w->clact_sys,
				&task_w->clact_res,
				cp_def->nclr, 1,
				cp_def->ncgr,
				cp_def->ncer,
				cp_def->nanr,
				ENCOUNT_ROCKET_CELL_CONT_ID
				);

		// アクター登録
		task_w->clact = ENC_CLACT_Add(
				&task_w->clact_sys, &task_w->clact_res,
				ENCOUNT_ROCKET_SPECIAL_MAT_X,
				ENCOUNT_ROCKET_SPECIAL_MAT_Y, 0, 0);
		CLACT_SetDrawFlag( task_w->clact, FALSE );
		CLACT_ObjModeSet( task_w->clact, GX_OAM_MODE_XLU );
		CLACT_BGPriorityChg( task_w->clact, 0 );
		GF_Disp_GX_VisibleControl( GX_PLANEMASK_OBJ, VISIBLE_ON );

		task_w->act_vec.x = ENCOUNT_ROCKET_SPECIAL_MAT_X;
		task_w->act_vec.y = ENCOUNT_ROCKET_SPECIAL_MAT_Y;
		task_w->act_vec.z = 0;

		// Alpha初期化
		G2_SetBlendAlpha( ENCOUNT_ROCKET_SPECIAL_ALPHA_PLANE_1, ENCOUNT_ROCKET_SPECIAL_ALPHA_PLANE_2, 12, 4 );

		// デフォルトプライオリティ設定
		GF_BGL_PrioritySet( GF_BGL_FRAME2_M, 0 );
		GF_BGL_PrioritySet( GF_BGL_FRAME3_M, 1 );
		GF_BGL_PrioritySet( GF_BGL_FRAME1_M, 2 );
		GF_BGL_PrioritySet( GF_BGL_FRAME0_M, 3 );

		GF_BGL_VisibleSet( GF_BGL_FRAME1_M, VISIBLE_OFF );
		GF_BGL_VisibleSet( GF_BGL_FRAME2_M, VISIBLE_OFF );
		GF_BGL_VisibleSet( GF_BGL_FRAME3_M, VISIBLE_OFF );

		// スクロールBGロード
		ENC_BG_SetNormalBG(
				eew->p_handle,
				NARC_field_encounteffect_r_kanbu_bg01_NSCR,
				NARC_field_encounteffect_r_kanbu_bg_NCGR,
				NARC_field_encounteffect_r_kanbu_bg_NCLR,
				0, 2, eew->fsw->bgl, GF_BGL_FRAME1_M );

		// トレーナーネーム生成
		{
			STRBUF* p_str;

			// パレット読み込み	(ジム用を流用)
			ArcUtil_HDL_PalSet( eew->p_handle,
					NARC_field_encounteffect_cutin_gym_font_NCLR,
					PALTYPE_MAIN_BG, ENCOUNT_SPTR_GYM_TRNAME_PAL*0x20, 0x20, heapID );

			// トレーナー名表示用ビットマップ作成
			GF_BGL_BmpWinAdd( eew->fsw->bgl, &task_w->trnamewin,
					GF_BGL_FRAME2_M, ENCOUNT_SPTR_GYM_TRNAME_X, ENCOUNT_SPTR_GYM_TRNAME_Y,
					ENCOUNT_SPTR_GYM_TRNAME_SIZX, ENCOUNT_SPTR_GYM_TRNAME_SIZY,
					ENCOUNT_SPTR_GYM_TRNAME_PAL, ENCOUNT_SPTR_GYM_TRNAME_CGX );
			GF_BGL_BmpWinDataFill( &task_w->trnamewin, 0 );
			OS_TPrintf("load trname no :%d \n", cp_def->trno );
			p_str = EncountEffect_Rocket_TrTypeNameGet( cp_def->trno, heapID );
			GF_STR_PrintColor( &task_w->trnamewin, FONT_SYSTEM,
										   p_str, 0, 0, MSG_ALLPUT,
										   ENCOUNT_SPTR_GYM_TRNAME_COL, NULL);
			STRBUF_Delete( p_str );	// 破棄わすれ注意！
		}

		// BG「R」ロード
		ENC_BG_SetNormalBG(
					eew->p_handle,
					NARC_field_encounteffect_r_kanbu_bg02_NSCR,
					NARC_field_encounteffect_r_kanbu_bg_NCGR,
					NARC_field_encounteffect_r_kanbu_bg_NCLR,
					0, 2, eew->fsw->bgl, GF_BGL_FRAME3_M );

		// ハードウェアウィンドウ 設定
		G2_SetWnd0InsidePlane( GX_WND_PLANEMASK_BG3, TRUE );
		G2_SetWndOutsidePlane( GX_WND_PLANEMASK_BG0|GX_WND_PLANEMASK_BG1|GX_WND_PLANEMASK_BG2|GX_WND_PLANEMASK_OBJ, TRUE );
		G2_SetWnd0Position( 0, GX_LCD_SIZE_Y/2, GX_LCD_SIZE_X-1, GX_LCD_SIZE_Y/2);

		ENC_Laster_Init( &task_w->laster, HEAPID_FIELD );

		EncountFlashTask(MASK_MAIN_DISPLAY, 16, -16,  &eew->wait, 1);

		eew->seq ++;
		break;

#if 1
	case ENCOUNT_ROCKET_SPECIAL_SCROLL_INI:
		GF_BGL_VisibleSet( GF_BGL_FRAME1_M, VISIBLE_ON );
		task_w->wait = ENCOUNT_ROCKET_SPECIAL_SCROLL_SYNC;
		eew->seq++;
		/* fallthlow */

	case ENCOUNT_ROCKET_SPECIAL_SCROLL:
		GF_BGL_ScrollSet( eew->fsw->bgl, GF_BGL_FRAME1_M, GF_BGL_SCROLL_X_INC, ENCOUNT_ROCKET_SPECIAL_SCROLL_SPEED );

		if( --task_w->wait <= 0 ) {
			eew->seq++;
		}
		break;
#endif

	case ENCOUNT_ROCKET_SPECIAL_MASK_INI:
		// 白黒「R」マーク表示
		GX_SetVisibleWnd( GX_WNDMASK_W0 );
		GF_BGL_VisibleSet( GF_BGL_FRAME3_M, VISIBLE_ON );
		ENC_AddMoveReqFx( &task_w->addmove_fx, 0, (GX_LCD_SIZE_Y/2)*FX32_ONE, 4*FX32_ONE, ENCOUNT_ROCKET_SPECIAL_MASK_SYNC );
		eew->seq++;
		/* fallthlow */

	case ENCOUNT_ROCKET_SPECIAL_MASK:
		GF_BGL_ScrollSet( eew->fsw->bgl, GF_BGL_FRAME1_M, GF_BGL_SCROLL_X_INC, ENCOUNT_ROCKET_SPECIAL_SCROLL_SPEED );

		{
			enum{ LCD_Y_CENTER = GX_LCD_SIZE_Y / 2, };

			if( ENC_AddMoveMainFx( &task_w->addmove_fx ) == FALSE ) {
				int y1 = LCD_Y_CENTER - (task_w->addmove_fx.x >> FX32_SHIFT);
				int y2 = LCD_Y_CENTER + (task_w->addmove_fx.x >> FX32_SHIFT);

				G2_SetWnd0Position( 0, y1, GX_LCD_SIZE_X-1, y2);
			} else {
				// ハードウェアウィンドウ 破棄
				GX_SetVisibleWnd( GX_WNDMASK_NONE );
				// 位置を補正
				GF_BGL_ScrollSet( eew->fsw->bgl, GF_BGL_FRAME1_M, GF_BGL_SCROLL_X_SET, 0 );
				task_w->wait = ENCOUNT_ROCKET_SPECIAL_MASKWAIT_SYNC;
				eew->seq++;
			}
		}
		break;

	case ENCOUNT_ROCKET_SPECIAL_MASKWAIT:
		if ( --task_w->wait <= 0 ) {
			eew->seq++;
		}
		break;

	case ENCOUNT_ROCKET_SPECIAL_ALPHA_INI :
		// BG：着色後の「R」をロード
		Scrn_OverWrite( eew, GF_BGL_FRAME1_M, NARC_field_encounteffect_r_kanbu_bg05_NSCR );

		// プライオリティ入れ替え
		GF_BGL_PrioritySet( GF_BGL_FRAME1_M, 1 );
		GF_BGL_PrioritySet( GF_BGL_FRAME3_M, 2 );

		// Alpha設定
		G2_SetBlendAlpha( ENCOUNT_ROCKET_SPECIAL_ALPHA_PLANE_1, ENCOUNT_ROCKET_SPECIAL_ALPHA_PLANE_2, 0, 16 );

		// OBJ表示
		CLACT_SetDrawFlag( task_w->clact, TRUE );

		task_w->wait = 0;
		eew->seq++;
		/* fallthlow */

	case ENCOUNT_ROCKET_SPECIAL_CUTIN :
		{
			BOOL b_alpha_end = FALSE;

			// Alpha
			if( task_w->wait <= 16 ) {

				task_w->wait += ENCOUNT_ROCKET_SPECIAL_ALPHA_SPEED;

				if( task_w->wait > 16 )
				{
					task_w->wait = 16;
					b_alpha_end = TRUE;
				}

#if 1
				// Alpha設定
				G2_SetBlendAlpha(
						ENCOUNT_ROCKET_SPECIAL_ALPHA_PLANE_1,
						ENCOUNT_ROCKET_SPECIAL_ALPHA_PLANE_2, task_w->wait, 16-task_w->wait );
#endif
			}

			// スピード指定
			task_w->act_vec.x += (GX_LCD_SIZE_X/2*FX32_ONE - task_w->act_vec.x) * 2 / 3;
			if( (task_w->act_vec.x >> FX32_SHIFT) <= GX_LCD_SIZE_X/2+2 ) {
				task_w->act_vec.x = GX_LCD_SIZE_X/2*FX32_ONE;

				// 両方の演出が終了したら次のシーケンスへ
				if( b_alpha_end == TRUE ) {

					// FRAME3をBG化
//					ENC_BG_SetAffineCont( eew->fsw->bgl );

					// フラッシュリクエスト
					FLUSH_SET( ENCOUNT_ROCKET_SPECIAL_CUTFLUSH_SYNC, BRIGHTNESS_NORMAL, BRIGHTNESS_WHITE );

					// バックドロップ面を黒くする
					GF_BGL_BackGroundColorSet( GF_BGL_FRAME0_M, 0x14a5 );
					GF_BGL_VisibleSet( GF_BGL_FRAME0_M, VISIBLE_OFF );

#if 0
					// ラスター開始
					ENC_Laster_Start( &task_w->laster, 0, 191, ((0xffff/192)*5), FX32_CONST(2), 400,
						LASTER_SCROLL_MBG1, 0, (TCB_TSK_PRI-1) );
					task_w->laster_flg = TRUE;
#endif

					eew->seq++;
				}
			}
		}

		// BGが回り込まないタイミングでトレーナー名表示
		if( task_w->wait > ENCOUNT_ROCKET_SPECIAL_ALPHA_SPEED ) {
			GF_BGL_VisibleSet( GF_BGL_FRAME2_M, VISIBLE_ON );
		}

		// トレーナー名：ジムリーダーOAMの位置を基準に移動
		GF_BGL_ScrollReq(
				eew->fsw->bgl, GF_BGL_FRAME2_M, GF_BGL_SCROLL_X_SET,
				-((task_w->act_vec.x>>FX32_SHIFT) + ENCOUNT_SPTR_GYM_TRNAME_OFS_X) );

		CLACT_SetMatrix( task_w->clact, &task_w->act_vec );
		break;

	case ENCOUNT_ROCKET_SPECIAL_CUTFLASH :
		if( IsFinishedBrightnessChg( MASK_MAIN_DISPLAY ) ) {
//			Scrn_OverWrite( eew, GF_BGL_FRAME3_M, NARC_field_encounteffect_r_kanbu_bg02_NSCR );

			task_w->wait = ENCOUNT_ROCKET_SPECIAL_CUTWAIT_SYNC;
			eew->seq++;
		}
		break;

	case ENCOUNT_ROCKET_SPECIAL_CUTWAIT :
		if( --task_w->wait <= 0 ) {
			fx32 def_x = task_w->act_vec.x;
			ENC_AddMoveReqFx( &task_w->addmove_fx, def_x, -12*8*FX32_ONE, 2*FX32_ONE, ENCOUNT_ROCKET_SPECIAL_CUTOUT_SPEED );
			task_w->wait = 8;
			GF_BGL_VisibleSet( GF_BGL_FRAME3_M, VISIBLE_OFF );

			eew->seq++;
		}
		break;

	case ENCOUNT_ROCKET_SPECIAL_CUTOUT :
		HOSAKA_PRINT("wait:%d\n",task_w->wait);
		task_w->act_vec.x = task_w->addmove_fx.x;

		if( -((task_w->act_vec.x>>FX32_SHIFT) + ENCOUNT_SPTR_GYM_TRNAME_OFS_X ) < 0 ) {
			// トレーナー名：ジムリーダーOAMの位置を基準に移動
			GF_BGL_ScrollReq(
				eew->fsw->bgl, GF_BGL_FRAME2_M, GF_BGL_SCROLL_X_SET,
				-((task_w->act_vec.x>>FX32_SHIFT) + ENCOUNT_SPTR_GYM_TRNAME_OFS_X) );
		} else {
			GF_BGL_VisibleSet( GF_BGL_FRAME2_M, VISIBLE_OFF );
		}


		CLACT_SetMatrix( task_w->clact, &task_w->act_vec );

		if( ENC_AddMoveMainFx( &task_w->addmove_fx ) ) {
			// 下画面非表示
			GXS_DispOff();
			// バックドロップ面を黒くする
			GF_BGL_BackGroundColorSet( GF_BGL_FRAME0_S, 0 );

			// ホワイトアウト開始
			WIPE_SYS_Start(
					WIPE_PATTERN_WMS, WIPE_TYPE_FADEOUT, WIPE_TYPE_FADEOUT,
					WIPE_FADE_WHITE, 4, 1, HEAPID_FIELD );
			eew->seq++;
		}
		break;

	case ENCOUNT_ROCKET_SPECIAL_WHITEOUT :
		if( WIPE_SYS_EndCheck() ) {
			eew->seq++;
		}
		break;

	case ENCOUNT_ROCKET_SPECIAL_END:
//		WIPE_SetBrightnessFadeOut( WIPE_FADE_BLACK );

		if(eew->end != NULL){
			*(eew->end) = TRUE;		// タスク終了を報告
		}

		ENC_Laster_Delete( &task_w->laster );
		task_w->laster_flg = FALSE;

		// OAM破棄
		CLACT_Delete( task_w->clact );
		ENC_CLACT_ResDeleteEasy( &task_w->clact_sys, &task_w->clact_res );
		ENC_CLACT_Delete( &task_w->clact_sys );

		// BMP
		GF_BGL_BmpWinDel( &task_w->trnamewin );

		// スクリーンクリーン
		GF_BGL_ClearCharSet( GF_BGL_FRAME3_M, 32, 0, HEAPID_FIELD );
		GF_BGL_ClearCharSet( GF_BGL_FRAME1_M, 32, 0, HEAPID_FIELD );
		GF_BGL_ScrClear( eew->fsw->bgl, GF_BGL_FRAME3_M );
		GF_BGL_ScrClear( eew->fsw->bgl, GF_BGL_FRAME1_M );

		ENC_End( eew, tcb );
		break;
	}

	// ラスターフリップ機能
	if( task_w->laster_flg == TRUE ){
		ENC_Laster_BuffXFlip( &task_w->laster, 2 );
	}

	if( eew->seq != ENCOUNT_ROCKET_SPECIAL_END ){
		CLACT_Draw( task_w->clact_sys.cas );
	}
}